package com.bw.ppjokeplay.commen.mvvm.view


import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.BarUtils
import com.bw.ppjokeplay.App
import com.bw.ppjokeplay.commen.field.UIField
import com.bw.ppjokeplay.commen.mvvm.viewmodel.BaseViewModel
import java.lang.reflect.ParameterizedType

/**
 * @author Lishaohe
 * @description:
 * @date :2021/11/9 0009 下午 6:27
 *
 */
abstract class BaseActivity<V : ViewDataBinding,VM : BaseViewModel<*>> : AppCompatActivity(),IView {
    protected lateinit var v : V
    protected lateinit var vm : VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        BarUtils.transparentStatusBar(this)
        BarUtils.setStatusBarLightMode(this, true)
        initViewModel()
        lifecycle.addObserver(vm)
        //livedata关联
        vm.getUIChange().showDialogLiveData!!.observe(this,showDialogObserver)
        vm.getUIChange().hideDialogLiveData!!.observe(this,hideDialogObserver)
        vm.getUIChange().showMsgLiveData!!.observe(this,showMsgObserver)
        vm.getUIChange().finishPageLiveData!!.observe(this,finishPageObserver)
        vm.getUIChange().startPageLiveData!!.observe(this,startPageObserver)
        bindDataBinding()
        v.setVariable(initVerable(),vm)
    }

    private var showDialogObserver : Observer<String> = object : Observer<String>{
        override fun onChanged(t: String?) {
            showDialog()
        }

    }

    private var hideDialogObserver : Observer<String> = object : Observer<String>{
        override fun onChanged(t: String?) {
            hideDialog()
        }

    }

    private var showMsgObserver : Observer<String> = object : Observer<String>{
        override fun onChanged(t: String?) {
            showMsg(t!!)
        }

    }

    private var finishPageObserver : Observer<String> = object : Observer<String>{
        override fun onChanged(t: String?) {
            finishPage()
        }

    }

    private var startPageObserver : Observer<HashMap<String,*>> = object : Observer<HashMap<String,*>>{
        override fun onChanged(t: HashMap<String,*>?) {
            var clazz : Class<*> = t!!.get(UIField.PAGECLAZZ) as Class<*>
            var bundle : Bundle = t!!.get(UIField.PAGEBUNDLE) as Bundle
            startPage(clazz,bundle)
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        vm.getUIChange().showDialogLiveData!!.removeObserver(showDialogObserver)
        vm.getUIChange().hideDialogLiveData!!.removeObserver(hideDialogObserver)
        vm.getUIChange().showMsgLiveData!!.removeObserver(showMsgObserver)
        vm.getUIChange().finishPageLiveData!!.removeObserver(finishPageObserver)
        vm.getUIChange().startPageLiveData!!.removeObserver(startPageObserver)
        lifecycle.removeObserver(vm)
        v.unbind()
    }

    override fun showDialog() {
    }

    override fun hideDialog() {
    }

    override fun showMsg(msg: String) {
        Toast.makeText(App.instance(),msg,Toast.LENGTH_SHORT).show()
    }

    override fun startPage(clazz: Class<*>, bundle: Bundle) {
        val intent : Intent = Intent(this,clazz)
        if (bundle!=null)
            intent.putExtras(bundle)
        startActivity(intent)
    }

    override fun finishPage() {
        finish()
    }

    override fun initViewModel() {
        val parameterizedType = this.javaClass.genericSuperclass as ParameterizedType
        val types = parameterizedType.actualTypeArguments
        val clazz : Class<VM> = types[1] as Class<VM>
        vm = ViewModelProvider(this,
            ViewModelProvider.NewInstanceFactory()).get(clazz)
    }

    override fun bindDataBinding() {
        v = DataBindingUtil.setContentView(this,bindLayout())
    }
}